#include "widget.h"
#include "./ui_widget.h"

#include <QString>
#include <QVBoxLayout>


CompleteEvent::CompleteEvent()
    : QEvent(EventType_Complete)
{}

bool WorkerBtn::event(QEvent* event)
{
    if (event->type() == EventType_Complete)
    {
        this->deleteLater();
        qDebug() << "recevied a CompleteEvent...";
        event->accept();
        qDebug() << "have managed the CompleteEvent...";
        return true;
    }
    return QPushButton::event(event);
}

WorkerBtn::WorkerBtn(QWidget* parent)
    : QPushButton(parent)
{
    this->setFixedSize(QSize(50, 50));

    QString style = "QPushButton { background-color: rgb(231, 231, 231); }";
    style += "QPushButton::pressed { background-color: rgb(210, 210, 210); }";
    this->setStyleSheet(style);

    this->setText("点击");
}

void WorkerBtn::mousePressEvent(QMouseEvent* event)
{
    // 1. 创建一个 CompleteEvent
    CompleteEvent* complete_event = new CompleteEvent();
    // 2. 异步调用
    QCoreApplication::postEvent(this, complete_event); // 参数：接收者，事件
}


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    this->setFixedSize(QSize(105, 105));

    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    this->setLayout(layout);

    btn = new WorkerBtn(this);
    layout->addWidget(btn);
}

Widget::~Widget()
{
    delete ui;
}
